package com.sun.dao;

import com.sun.dto.TagAndAmount;
import com.sun.pojo.Tag;
import com.sun.pojo.Type;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author: glorysun
 * @date: 2020/8/5
 */
@Mapper
@Repository
public interface TagDao {

    @Insert("insert into blog.t_tag values (#{id},#{name})")
    int insertTag(Tag tag);   //新增标签

    @Select("select * from blog.t_tag where id=#{id}")
    Tag selectTagById(Long id);  //根据id查询标签

    @Select("select * from blog.t_tag where name=#{name}")
    Tag selectTagByName(String name);  //根据标签名查询标签

    @Select("SELECT t.*\n" +
            "FROM t_blog_tags bt LEFT JOIN t_tag t ON bt.tag_id=t.id\n" +
            "where bt.blog_id=#{id}")
    List<Tag> selectTagByBlogId(Long id);

    @Select("select * from blog.t_tag")
    List<Tag> selectTagList();  //分页查询标签

    @Update(" update blog.t_tag set name = #{name} where id = #{id};")
    int updateTag(Tag tag);     //更新标签

    @Delete("delete from blog.t_tag where id = #{id}")
    int deleteTag(Long id);    //删除标签

    @Select("<script>select bt.tag_id,count(bt.tag_id) as amount,t.name\n" +
            "from t_blog_tags bt,t_tag t\n" +
            "where bt.tag_id=t.id\n" +
            "GROUP BY bt.tag_id\n" +
            "ORDER BY amount desc </script>")
    List<TagAndAmount> selectTagAndAmount();    //查询标签以及博客数量

    @Delete("delete from blog.t_blog_tags where blog_id=#{id}")
    int deleteTagByBlogId(Long id);   //根据博客id删除标签

}
